/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.elec.web;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.excel.ExportExcel;
import cn.net.ecode.common.utils.excel.ImportExcel;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.elec.entity.ElecCloseAccounts;
import cn.net.ecode.elec.service.ElecCloseAccountsService;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 电量结算Controller
 * @author xiaodunshan
 * @version 2019-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/elec/elecCloseAccounts")
public class ElecCloseAccountsController extends BaseController {

	@Autowired
	private ElecCloseAccountsService elecCloseAccountsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ElecCloseAccounts get(String id, boolean isNewRecord) {
		return elecCloseAccountsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("elec:elecCloseAccounts:view")
	@RequestMapping(value = {"list", ""})
	public String list(ElecCloseAccounts elecCloseAccounts, Model model) {
		model.addAttribute("elecCloseAccounts", elecCloseAccounts);
		return "ecode/elec/elecCloseAccountsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("elec:elecCloseAccounts:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<ElecCloseAccounts> listData(ElecCloseAccounts elecCloseAccounts, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ElecCloseAccounts> page = elecCloseAccountsService.findPage(new Page<ElecCloseAccounts>(request, response), elecCloseAccounts);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("elec:elecCloseAccounts:view")
	@RequestMapping(value = "form")
	public String form(ElecCloseAccounts elecCloseAccounts, Model model) {
		model.addAttribute("elecCloseAccounts", elecCloseAccounts);
		return "ecode/elec/elecCloseAccountsForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("elec:elecCloseAccounts:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(ElecCloseAccounts elecCloseAccounts) {
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, elecCloseAccounts)){
			return renderResult(Global.FALSE, message);
		}
		elecCloseAccountsService.save(elecCloseAccounts);
		return renderResult(Global.TRUE, "保存电量结算成功！");
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("elec:elecCloseAccounts:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(ElecCloseAccounts elecCloseAccounts) {
		elecCloseAccountsService.delete(elecCloseAccounts);
		return renderResult(Global.TRUE, "删除电量结算成功！");
	}



	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "导入模板.xlsx";
			List<ElecCloseAccounts> list = Lists.newArrayList();
			new ExportExcel("结算电量数据模板", ElecCloseAccounts.class, 2).setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/elec/elecCloseAccounts/list?repage";
	}


	/**
	 * @param file
	 * @param redirectAttributes
	 * @return
	 * @Title: importFile
	 * @Description: 导入数据
	 */
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		if (Global.isDemoMode()) {
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/mdm/member/?repage";
		}
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<ElecCloseAccounts> list = ei.getDataList(ElecCloseAccounts.class);
			for (ElecCloseAccounts elecCloseAccounts : list) {
				try {
					elecCloseAccounts.preInsert();

					elecCloseAccountsService.insert(elecCloseAccounts);
					successNum++;
				} catch (Exception ex) {
					failureMsg.append("<br/>导入失败：" + ex.getMessage());
					System.out.println("导入结算数据失败！失败信息：" + ex.getMessage());

					failureNum++;
				}
			}
			if (failureNum > 0) {
				failureMsg.insert(0, "，失败 " + failureNum + " 条结算数据信息，导入信息如下：");
				addMessage(redirectAttributes, "已成功导入 " + successNum + " 条结算数据信息" + failureMsg + "<br/>");
			} else {
				addMessage(redirectAttributes, "已成功导入 " + successNum + " 条结算数据信息" + failureMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "导入结算数据失败！失败信息：" + e.getMessage());
			System.out.println("导入结算数据失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/elec/elecCloseAccounts/list?repage";
	}
}